package com.insigma.business.qrcode.intell._provider.impl;

import com.insigma.business.qrcode.common.converter.CompanyEntryApplyProviderConverter;
import com.insigma.business.qrcode.common.template.BasePagingResponse;
import com.insigma.business.qrcode.common.util.BizCheckUtil;
import com.insigma.business.qrcode.common.util.ResponseUtils;
import com.insigma.business.qrcode.intell._provider.CompanyEntryApplyProvider;
import com.insigma.business.qrcode.intell.dto.CompanyEntryApplyDTO;
import com.insigma.business.qrcode.intell.dto.CompanyEntryApplySearchRequest;
import com.insigma.business.qrcode.soldier.dto.CompanyEntryApplyBSearcher;
import com.insigma.business.qrcode.soldier.dto.gov.BaseProviderImpl;
import com.insigma.business.qrcode.common.template.area.ScenarioCodeEnum;
import com.insigma.business.qrcode.common.template.area.ServiceCallback;
import com.insigma.business.qrcode.soldier.entity.CompanyEntryApplyDO;
import com.insigma.business.qrcode.soldier.service.CompanyEntryApplyService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class CompanyEntryApplyProviderImpl extends BaseProviderImpl implements CompanyEntryApplyProvider {

    @Autowired
    private CompanyEntryApplyService companyEntryApplyService;

    @Override
    public BasePagingResponse<List<CompanyEntryApplyDTO>> searchCompanyEntryApply(CompanyEntryApplySearchRequest searchRequest) {
        final BasePagingResponse<List<CompanyEntryApplyDTO>> basePagingResponse = new BasePagingResponse<>();
        serviceTemplate.execute(basePagingResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(searchRequest, "查询分页CompanyEntryApply的请求参数不能为空");
            }

            @Override
            public void executeService() {
                if (Strings.isBlank(searchRequest.getSortKeyValueJoint())) {
                    searchRequest.setSortKeyValueJoint("createDatetime_desc");
                }

                final CompanyEntryApplyBSearcher companyEntryApplyBSearcher = CompanyEntryApplyProviderConverter.convertRequestSO2Searcher(searchRequest);
                final List<CompanyEntryApplyDO> companyEntryApplyDOList = companyEntryApplyService.search(companyEntryApplyBSearcher);
                final List<CompanyEntryApplyDTO> companyEntryApplyDTOList = CompanyEntryApplyProviderConverter.convertResponseDOList2DTOList(companyEntryApplyDOList);
                for (CompanyEntryApplyDTO applyDTO : companyEntryApplyDTOList) {
                    applyDTO.setIsPubPosition(Strings.isNotBlank(applyDTO.getPositionId()));
                }

                ResponseUtils.buildSuccessPagingResponse(basePagingResponse, companyEntryApplyDTOList, searchRequest, companyEntryApplyBSearcher.getTotalCount());
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMPANY_ENTRY_APPLY_SEARCH;
            }
        });

        return basePagingResponse;
    }
}
